package org.itboys.kitchen.service.mysql.order;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.entity.lasted.SystemUser;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.admin.service.lasted.SystemUserService;
import org.itboys.admin.tools.AdminSessionHolder;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.framework.resource.ResourceHolder;
import org.itboys.kitchen.dao.order.KitchenOrderProductMapper;
import org.itboys.kitchen.entity.mysql.order.KitchenOrder;
import org.itboys.kitchen.entity.mysql.order.KitchenOrderProduct;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.*;


/**
 * 作者：jiangxiong
 * 日期：2016年11月25日
 * 联系方式 ：empty90911@126.com
 * 描述：农厨_订单_后台_service
 */
@Service
public class KitchenOrderService extends BaseMySqlService<KitchenOrder> {
	@Autowired
	private KitchenOrderProductMapper orderProductMapper;
	@Autowired
	private SundayUploadService uploadService;
	@Autowired
	private SystemUserService userService;


	/**
	 * 分页获取订单
	 * @param request
	 * @param type
	 * @return
	 */
	public PageResult<KitchenOrder> selectOrder(HttpServletRequest request, Integer type){
		Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"orderNo","memberName","shopName","orderStatus","userType");
		param.put("type",type);
		//2017年7月3日，以登陆后台的门店，过滤对应的订单
		if(type==1){
			Long userId = AdminSessionHolder.getAdminUserId();
			logger.info("---当前登陆用户ID-订单查询="+userId);
			if(LongUtil.isNotZreo(userId)){
				SystemUser user = userService.getById(userId);
				logger.info("---当前登陆用户ID--平台用户ID----订单查询"+user.getPlatformMemberId());
				if(user.getPlatformMemberId()!=null){
					param.put("shopId",user.getPlatformMemberId());

				}
			}
		}
	return  new PageResult<KitchenOrder>(super.select(param),super.count(param));
	}

	/**
	 * 查询订单详情
	 * @param orderId
	 * @return
	 */
	public KitchenOrder findOne(Long orderId,boolean isPackageProduct){
		KitchenOrder order = super.find(orderId);
		if(isPackageProduct){
			Map<String,Object> param = new HashMap<String,Object>();
			param.put("orderId",orderId);
			order.setProducts(orderProductMapper.select(param));
		}
		return order;
	}
	/**
	 * 分页获取订单商品
	 * @param request
	 * @param orderId
	 * @return
	 */
	public PageResult<KitchenOrderProduct> selectProduct(HttpServletRequest request, Long orderId ){
		Map<String,Object> param = ParamUtil.packageMysqlPageParam(request,"");
		param.put("orderId",orderId);
		Serializable count = orderProductMapper.count(param);
		Long countResult = count==null?0l: Long.valueOf(count.toString());
		return  new PageResult<KitchenOrderProduct>(orderProductMapper.select(param),countResult);
	}
	/**
	 * 配送采购订单
	 * @param orderId
	 * @return
	 */
	public void sendOrder_caigou(Long orderId, String driverName, String driverMobile, String driverCarNo, MultipartFile driverImageFile) throws  Exception {

		KitchenOrder order = super.find(orderId);
		if(order.getType()==3&&order.getOrderStatus()==2){
			order.setOrderStatus(3);
			order.setDriverName(driverName);
			order.setDriverMobile(driverMobile);
			order.setDriverCarNo(driverCarNo);
			//保存司机图片
			Map<String, String> map = uploadService.uploadSingle(driverImageFile);
			if(StringUtils.isNotEmpty(map.get(uploadService.UPLOAD_HALF_PATH))){
				order.setDriverImage(map.get(uploadService.UPLOAD_HALF_PATH));
			}
			order.setSendTime(new Date());
			super.update(order);
		}

	}
	/**
	 *
	 * @param orderId
	 * @return
	 */
	/**
	 * 配送积分订单
	 * @param orderId
	 * @param driverName 快递公司
	 * @param driverMobile 快递公司编码
	 * @param driverCarNo 快递单号
	 * @throws Exception
	 */
	public void sendOrder_score(Long orderId, String driverName, String driverMobile, String driverCarNo) {
		KitchenOrder order = super.find(orderId);
		if(order.getType()==2&&order.getOrderStatus()==2){
			order.setOrderStatus(3);
			order.setDriverName(driverName);
			order.setDriverMobile(driverMobile);
			order.setDriverCarNo(driverCarNo);
			order.setSendTime(new Date());
			super.update(order);
		}
	}


}
